package com.frxuexi.mdm2201

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.fragment.app.Fragment
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.mdm2201.databinding.ActivityMainBinding
import com.frxuexi.mdm2201.fragment.HomeFragment
import com.frxuexi.mdm2201.fragment.MessageFragment
import com.frxuexi.mdm2201.view.MainPage
import com.frxuexi.mine.MineFragment
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

@Route(path = "/app/MainActivity")
class MainActivity : AppCompatActivity() {
//    lateinit var com:ComposeView
    lateinit var binding:ActivityMainBinding
    @SuppressLint("CoroutineCreationDuringComposition")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding= ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
//        com=findViewById(R.id.com)

        var route="welcome"
        val kv = MMKV.defaultMMKV()
        /*val isIn = kv.decodeBool("isIn", false)
        if(isIn){
            route="main"
        }*/

        //取出上一次的进入时间
        val preTime = kv.decodeLong("time", 0)
        //获取当前时间
        val currentTime = System.currentTimeMillis()
        //间隔时间24小时
        val time=(currentTime-preTime)/1000/60/60
        if(time>=24){//欢迎页
            //route="welcome"
            binding.com.visibility=View.VISIBLE
            binding.tab.visibility=View.GONE
            binding.frameView.visibility=View.GONE
        }else{//主页面
            //route="main"
//            binding.com.visibility=View.GONE
//            binding.tab.visibility=View.VISIBLE
//            binding.frameView.visibility=View.VISIBLE
            isLogin()
        }

        binding.com.setContent {
//            WelcomePage()

            /*val navController = rememberNavController()
            NavHost(navController = navController, startDestination = route){
                composable("welcome"){WelcomePage()}
                composable("main"){
                            MainPage()
                    }
                }*/

            lifecycleScope.launch {
                val timer = async { delay(3000) }
                timer.await()
//                kv.encode("isIn",true)
                //记录打开/进入app的时间
                kv.encode("time",System.currentTimeMillis())
//                ARouter.getInstance().build("/apps/NetAppActivity").navigation()
//                navController.navigate("main")
//                binding.com.visibility=View.GONE
//                binding.tab.visibility=View.VISIBLE
//                binding.frameView.visibility=View.VISIBLE
                isLogin()
            }
        }

        val messageFragment = ARouter.getInstance().build("/app/MessageFragment").navigation() as MessageFragment
        val homeFragment = ARouter.getInstance().build("/app/HomeFragment").navigation() as HomeFragment
        val mineFragment = ARouter.getInstance().build("/mine/MineFragment").navigation() as MineFragment

        val fragments = listOf<Fragment>(
            messageFragment, homeFragment, mineFragment
        )

//        binding.tab.addTab(binding.tab.newTab().setText("消息"))
//        binding.tab.addTab(binding.tab.newTab().setText("门户"))
//        binding.tab.addTab(binding.tab.newTab().setText("我的"))

        showFragment(homeFragment)
        val tab = binding.tab.newTab().setText("门户")
        binding.tab.apply {
            addTab(newTab().setText("消息"))
            addTab(tab)
            addTab(newTab().setText("我的"))

            addOnTabSelectedListener(object :OnTabSelectedListener{
                override fun onTabSelected(tab: TabLayout.Tab?) {
                    showFragment(fragments.get(tab?.position!!))
                }

                override fun onTabUnselected(tab: TabLayout.Tab?) {

                }

                override fun onTabReselected(tab: TabLayout.Tab?) {

                }

            })
        }
        binding.tab.selectTab(tab)

    }

    override fun onResume() {
        super.onResume()
        isLogin()
    }

    fun isLogin(){
        val isLogin = MMKV.defaultMMKV().decodeString("name", "")
        if(isLogin.isNullOrEmpty()){
            ARouter.getInstance().build("/user/LoginActivity").navigation()
        }else{
            binding.com.visibility=View.GONE
            binding.tab.visibility=View.VISIBLE
            binding.frameView.visibility=View.VISIBLE
        }
    }

    fun showFragment(fragment: Fragment){
        val transaction = supportFragmentManager.beginTransaction()
        transaction.replace(R.id.frame_view,fragment)
        transaction.commit()
    }

}



@Preview
@Composable
fun WelcomePage(){
    Column(modifier = Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
        Spacer(modifier = Modifier.height(200.dp))
        Image(modifier = Modifier.size(60.dp), painter = painterResource(id = R.drawable.img), contentDescription = "")
        Spacer(modifier = Modifier.height(10.dp))
        Text(text = "MDM")
    }
}